package com.aici.dashboardHandle.service.impl;

import com.aici.dashboardHandle.convert.*;
import com.aici.dashboardHandle.domain.*;
import com.aici.dashboardHandle.dto.*;
import com.aici.dashboardHandle.mapper.TUserDashProjectRelaMapper;
import com.aici.dashboardHandle.mapper.TDashboardMapper;
import com.aici.dashboardHandle.mapper.TDashCanvasRelaMapper;
import com.aici.dashboardHandle.mapper.TCanvasMapper;
import com.aici.dashboardHandle.mapper.TCanvasComponentRelaMapper;
import com.aici.dashboardHandle.mapper.TComponentMapper;
import com.aici.dashboardHandle.mapper.TComponentDataMapper;
import com.aici.dashboardHandle.service.DashBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhongfs
 */
@Service
@Transactional
public class DashBoardServiceImpl implements DashBoardService {
    @Autowired
    private TUserDashProjectRelaMapper tUserDashProjectRelaMapper;
    @Autowired
    private TDashboardMapper tDashboardMapper;
    @Autowired
    private TDashCanvasRelaMapper tDashCanvasRelaMapper;
    @Autowired
    private TCanvasMapper tCanvasMapper;
    @Autowired
    private TCanvasComponentRelaMapper tCanvasComponentRelaMapper;
    @Autowired
    private TComponentMapper tComponentMapper;
    @Autowired
    private TComponentDataMapper tComponentDataMapper;

    @Override
    public List<TDashboardDTO> queryByTitleWithUserCode(String userCode, String title, Integer pageNo, Integer pageSize) {
        Integer offset = null;
        if (pageNo != null && pageSize != null) {
            offset = 0;
            if (pageNo > 1) {
                offset = (pageNo - 1) * pageSize;
            }
        }
        return TDashboardCnvt.INSTANCE.toDTOs(tDashboardMapper.queryByTitleWithUserCode(userCode, title, offset, pageSize));
    }

    @Override
    public boolean insertUserDashProjectRela(TUserDashProjectRelaDTO tUserDashProjectRelaDTO) {
        TUserDashProjectRelaEntity myTUserDashProjectRela = TUserDashProjectRelaCnvt.INSTANCE.toDomain(tUserDashProjectRelaDTO);
        tUserDashProjectRelaMapper.saveTUserDashProjectRelaSingleline(myTUserDashProjectRela);
        return true;
    }

    @Override
    public boolean insertDashboard(TDashboardDTO tDashboardDTO) {
        TDashboardEntity myTDashboard = TDashboardCnvt.INSTANCE.toDomain(tDashboardDTO);
        tDashboardMapper.saveTDashboardSingleline(myTDashboard);
        return true;
    }

    @Override
    public int queryCountByTitleWithUserCode(String title, String userCode) {
        return tDashboardMapper.queryCountByTitleWithUserCode(title, userCode);
    }

    @Override
    public boolean insertDashCanvasRela(TDashCanvasRelaDTO tDashCanvasRelaDTO) {
        TDashCanvasRelaEntity myTDashCanvasRela = TDashCanvasRelaCnvt.INSTANCE.toDomain(tDashCanvasRelaDTO);
        tDashCanvasRelaMapper.saveTDashCanvasRelaSingleline(myTDashCanvasRela);
        return true;
    }

    @Override
    public boolean insertCanvas(TCanvasDTO tCanvasDTO) {
        TCanvasEntity myTCanvasEntity = TCanvasCnvt.INSTANCE.toDomain(tCanvasDTO);
        tCanvasMapper.saveTCanvasSingleline(myTCanvasEntity);
        return true;
    }

    @Override
    public boolean insertCanvasComponentRela(TCanvasComponentRelaDTO tCanvasComponentRelaDTO) {
        TCanvasComponentRelaEntity myTCanvasComponentRela = TCanvasComponentRelaCnvt.INSTANCE.toDomain(tCanvasComponentRelaDTO);
        tCanvasComponentRelaMapper.saveTCanvasComponentRelaSingleline(myTCanvasComponentRela);
        return true;
    }

    @Override
    public boolean insertComponent(TComponentDTO tComponentDTO) {
        TComponentEntity myTComponent = TComponentCnvt.INSTANCE.toDomain(tComponentDTO);
        tComponentMapper.saveTComponentSingleline(myTComponent);
        return true;
    }

    @Override
    public boolean insertComponentData(TComponentDataDTO tComponentDataDTO) {
        TComponentDataEntity myTComponentData = TComponentDataCnvt.INSTANCE.toDomain(tComponentDataDTO);
        tComponentDataMapper.saveTComponentDataSingleline(myTComponentData);
        return true;
    }

    @Override
    public int queryCntByTitleWithUserCode(String userCode, String title) {
        return tDashboardMapper.queryCntByTitleWithUserCode(userCode, title);
    }

    @Override
    public TDashboardDTO queryByDashId(String dashId) {
        TDashboardEntity tDashboard = tDashboardMapper.queryByDashId(dashId);
        return TDashboardCnvt.INSTANCE.toDTO(tDashboard);
    }

    //以下为删除功能接口
    @Override
    public boolean deleteDashboardByDashId(String dashId) {
        return tDashboardMapper.deleteDashboardByDashId(dashId);
    }

    @Override
    public boolean deleteUserDashRelaByDashId(String dashId) {
        return tUserDashProjectRelaMapper.deleteUserDashRelaByDashId(dashId);
    }

    @Override
    public String selectDashCanvasRelaByDashId(String dashId) {
        TDashCanvasRelaEntity dashCanvasRelaEntity = tDashCanvasRelaMapper.selectDashCanvasRelaByDashId(dashId);
        String canvasId = dashCanvasRelaEntity.getCanvasId();
        return canvasId;
    }

    @Override
    public boolean deleteDashCanvasRelaByCanvasId(String canvasId) {
        return tDashCanvasRelaMapper.deleteDashCanvasRelaByCanvasId(canvasId);
    }

    @Override
    public boolean deleteCanvasByCanvasId(String canvasId) {
        return tCanvasMapper.deleteCanvasByCanvasId(canvasId);
    }

    @Override
    public List<String> selectCanvasComponentRelaByCanvasId(String canvasId) {
        List<TCanvasComponentRelaEntity> canvasComponentRelaEntitys = tCanvasComponentRelaMapper.selectCanvasComponentRelaByCanvasId(canvasId);
        List<String> componentIds = new ArrayList<String>();
        for (TCanvasComponentRelaEntity canvasComponentRelaEntity : canvasComponentRelaEntitys) {
            String componentId = canvasComponentRelaEntity.getComponentId();
            componentIds.add(componentId);
        }
        return componentIds;
    }

    @Override
    public boolean deleteCanvasComponentRelaByComponentId(String componentId) {
        return tCanvasComponentRelaMapper.deleteCanvasComponentRelaByComponentId(componentId);
    }

    @Override
    public boolean deleteComponentByComponentId(String componentId) {
        return tComponentMapper.deleteComponentByComponentId(componentId);
    }

    @Override
    public boolean deleteComponentDataByComponentId(String componentId) {
        return tComponentDataMapper.deleteComponentDataByComponentId(componentId);
    }

}
